﻿/// <reference path="router.js" />
/// <reference path="config.js" />
'use strict';

// For any third party dependencies, like jQuery, place them in the assets folder.

var debug = true,
    // 'useMin' indicates if the app will load the min version of the right js files, or not.
    useMin = false;

var minUrl = (!useMin) ? '' : '.min';

// Configure loading modules from the assets directory,
// except for 'app' ones, which are in a sibling
// directory.
requirejs.config({
    //baseUrl: 'assets',
    paths: {
        assets: '../assets',
        common: '../common',
        js: '../js',
        screens: '../screens',

        backbone: '../assets/backbone/js/1.1.2' + minUrl,
        bootstrap: '../assets/bootstrap/js/3.1.1' + minUrl,
        handlebars: '../assets/handlebars/js/1.3.0' + minUrl,
        jquery: '../assets/jquery/js/1.11.1' + minUrl,
        'jquery.ui': '../assets/jquery-ui/js/1.11.0' + minUrl,
        underscore: '../assets/underscore/js/1.6.0' + minUrl,

        json2: '../assets/json2/1.0.0' + minUrl,
        moment: '../assets/moment/moment.2.10.3' + minUrl,
        sprintf: '../assets/sprintf/1.0.0' + minUrl,
        stacktrace: '../assets/stacktrace/1.0.0' + minUrl,

        jqGrid: '../assets/jqGrid/js/4.5.2' + minUrl,
        backgrid: '../assets/backgrid/js/backgrid.0.3.5' + minUrl,
        'backgrid.all': '../js/backgrid.extensions' + minUrl,
        datepicker: '../assets/bootstrap/plugins/datepicker/1.4.0/js/datepicker' + minUrl,
        select2: '../assets/select2/4.0.0/js/select2.full' + minUrl,
        typeahead: '../assets/bootstrap/plugins/typeahead/3.1.0/typeahead' + minUrl,

        config: '../js/config' + minUrl,
        router: '../js/router' + minUrl,
        baseClass: '../js/baseClass' + minUrl,
        response: '../js/response' + minUrl,

        utils: '../js/utils' + minUrl,
        'app.utils': '../js/app.utils' + minUrl,

        commonView: '../js/commonView' + minUrl,
        screenModel: '../js/screenModel' + minUrl,
        screenView: '../js/screenView' + minUrl,
        webServiceProxy: '../js/webServiceProxy' + minUrl,
    },
    shim: {
        'backbone': {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone',
        },
        'handlebars': {
            exports: 'Handlebars',
        },
        jquery: {
            exports: '$',
        },
        'jquery.ui': {
            deps: ['jquery'],
            exports: '$'
        },
        'underscore': {
            exports: '_',
        },

        'json2': {
            exports: 'json2',
        },
        'moment': {
            exports: 'moment',
        },
        'sprintf': {
            exports: 'sprintf',
        },

        'utils': {
            deps: ['underscore', 'json2', 'sprintf', 'baseClass'],
            exports: 'utils',
        },
        'commonView': {
            deps: ['jquery.ui', 'backbone', 'handlebars', 'utils'],
            exports: 'CommonView',
        },
        'screenModel': {
            deps: ['backbone', 'utils'],
            exports: 'ScreenModel',
        },
        'screenView': {
            deps: ['jquery.ui', 'backbone', 'handlebars', 'utils'],
            exports: 'ScreenView',
        },
        'webServiceProxy': {
            deps: ['handlebars', 'utils'],
            exports: 'WebServiceProxy',
        },

        'backgrid': {
            deps: ['backbone', 'underscore'],
            exports: 'Backgrid',
        },
    },
});


//Load main modules to initalize App global object.
//Then load the main js file.
require([
    'jquery',
    'underscore',
    'backbone',
    'config',
    'router',
    'app.utils',
    'js/managementServiceProxy' + ((!debug) ? '' : '.debug'),
], function ($, _, Backbone, Config, AppRouter, AppUtils, ManagementServiceProxy) {
    //Initialize App global object and attached to window scope.
    window.App = {
        config: Config,
        proxy: new ManagementServiceProxy(),
        router: new AppRouter({
            container: '#mainViewContainer',
            titleContainer: '#mainTitle',
            routesConfig: Config.routes,
        }),
        utils: AppUtils,
    };


    //// Start loading the main app file. Put all of
    //// your application logic in there.
    //requirejs(['js/main']);



    var initialRoute = $.trim(window.location.hash.replace('#', ''));

    //Start Backbone history api.
    Backbone.history.start({
        root: App.config.root,
        hashChange: true,
        pushState: false,
    });
    
    
    //Push initial route.
    if (initialRoute.length == 0)
        window.location.hash = "#" + App.config.defRoute;
    else
        window.location.hash = "#" + initialRoute;
});